﻿/**
 * 全局通用UI控件库扩展: 快捷登录注册弹出框
 * by Auntion | 2010-12-6
 * Auntion@Gmail.com
 * !需要auntion.js支持
 */

var at_login = function(){
    this.pub = $("body");
    this.loginTrace = new at_trace();
    this.loginBox = null;
    this.login = {
		msgbox: null,
        username: null,
        password: null,
        autologin: null,
        loginbut: null,
        enterreg: null
    };
    this.regTrace = new at_trace();
    this.regBox = null;
    this.reg = {
		msgbox: null,
        usernameHasReg: null,
        usernameMsg: null,
        username: null,
        password: null,
        passking: null,
        checkpass: null,
        mailHasReg: null,
        mailMsg: null,
        mail: null,
        agreement: null,
        regbut: null
    };
    this.infoTrace = new at_trace();
    this.infoBox = null;
    this.containerHTML = null;
    this.init();
}, at_login_loaded = true;
at_login.prototype = {
    init: function(){
        var B, A;
        this.containerHTML = "<div class=\"at_wlogin clearfix\"><div class=\"at_wl_i\"><u class=\"at_msg m_3_i\"></u><span>您未登陆，不能对店家发送站内信!</span></div></div>";
        this.loginBox = this.createLoginLayer();
        this.loginTrace.setWidth(490);
        this.loginTrace.setDefaultButtonDisplay("hide");
        B = this.loginBox.find("span.at_text");
        A = this.loginBox.find("a.at_but");
        this.login = {
			msgbox: this.loginBox.find(">.at_wl_i>span"),
            username: B.eq(0),
            password: B.eq(1),
            autologin: this.loginBox.find("input[type='checkbox']"),
            loginbut: A.eq(0),
            enterreg: A.eq(1)
        };
        this.regBox = this.createRegLayer();
        this.regTrace.setWidth(490);
        this.regTrace.setDefaultButtonDisplay("hide");
        B = this.regBox.find("span.at_text");
        A = this.regBox.find("a.at_but");
        this.reg = {
			msgbox: this.regBox.find(">.at_wl_i>span"),
            username: B.eq(0),
            password: B.eq(1),
            passking: B.eq(1).find("u.at_passking"),
            checkpass: B.eq(2),
            mail: B.eq(3),
            agreement: this.regBox.find("input[type='checkbox']"),
            regbut: A.eq(0)
        };
        this.infoBox = this.createInfoLayer();
        this.infoTrace.setWidth(350);
        this.bindEvent();
    },
	getRadomNumber: function() {
		return parseInt(String(Math.random()*1000000000000000).replace(/[^0-9]*/g,""));
	},
    createLoginLayer: function(){
        var A, B;
        B = "<div class=\"at_wl_l\">";
        B += "<p><span class=\"at_wl_lt gray2\">\u7528\u6237\u540d</span><span class=\"at_text t_1_d\"><input value=\"\" size=\"25\" maxlength=\"16\" /></span></p>";
        B += "<p><span class=\"at_wl_lt gray2\">\u5bc6\u3000\u7801</span><span class=\"at_text t_1_d\"><input value=\"\" size=\"25\" maxlength=\"16\" type=\"password\"/></span></p>";
        B += "<p><a href=\"javascript:void(0)\" class=\"at_but b_2_g\"><u></u>登录</a><span class=\"at_wl_ck ml15\"><input type=\"checkbox\" checked=\"true\"></span><span class=\"at_wl_ck\">一周内自动登录</span></p>";
        B += "</div>";
        B += "<div class=\"at_wl_e\">";
        B += "<p class=\"gray2\">没有账号？立即免费注册！</p>";
        B += "<p><a href=\"javascript:void(0)\" class=\"at_but bm0 b_1_y\"><u></u>\u7acb\u5373\u6ce8\u518c</a></p>";
        B += "</div>";
        this.loginTrace.setContact("会员登录", this.containerHTML);
        A = this.loginTrace.getThis().find("div.at_wlogin").append(B);
        return A;
    },
    createRegLayer: function(){
        var A, B;
        B = "<s class=\"e10\"></s>";
        B += "<p><span class=\"at_wl_lt gray2 w50\">\u767b\u9646\u8d26\u53f7</span><span class=\"at_text t_1_d\"><input value=\"\" size=\"25\" maxlength=\"16\"/></span></p>";
        B += "<p><span class=\"at_wl_lt gray2 w50\">\u8bbe\u7f6e\u5bc6\u7801</span><span class=\"at_text t_1_d\"><input value=\"\" size=\"25\" maxlength=\"16\" type=\"password\"/><em><u class=\"at_passking\"><s class=\"at_txt\">\u5b89\u5168\u7b49\u7ea7\uff1a</s><s class=\"at_bar\"><span></span><span></span><span class=\"at_end\"></span></s></u></em></span></p>";
        B += "<p><span class=\"at_wl_lt gray2 w50\">\u786e\u8ba4\u5bc6\u7801</span><span class=\"at_text t_1_d\"><input value=\"\" size=\"25\" maxlength=\"16\" type=\"password\"/></span></p>";
        B += "<p><span class=\"at_wl_lt gray2 w50\">\u90ae\u7bb1\u8d26\u6237</span><span class=\"at_text t_1_d\"><input value=\"\" size=\"25\" maxlength=\"100\"/></span></p>";
        B += "<p class=\"pb0\"><span class=\"at_wl_ck ckm\"><input type=\"checkbox\" checked=\"true\"></span><span class=\"at_wl_ck\">\u6211\u63a5\u53d7<a href=\"javascript:void(0)\" class=\"blue3\">\u732a\u516b\u6212\u7f51\u7528\u6237\u670d\u52a1\u534f\u8bae</a></span></p>";
        B += "<p><a href=\"javascript:void(0)\" class=\"at_but bm60 b_1_y\"><u></u>\u6ce8\u518c</a></p>";
        this.regTrace.setContact("\u5feb\u901f\u6ce8\u518c\u4f1a\u5458", this.containerHTML);
        A = this.regTrace.getThis().find("div.at_wlogin").append(B);
        return A;
    },
    createInfoLayer: function(){
        var A;
        A = "<div class=\"at_trace_if clearfix\"><u class=\"at_msg m_1_i\"></u><p class=\"at_trace_if_c ml15\">\u63d0\u793a\u5185\u5bb9</p></div>";
        this.infoTrace.setContact("\u7cfb\u7edf\u63d0\u793a", A, [this.infoTrace.getButton("返回", "gray", "close")]);
        return this.infoTrace.getThis().find("p.at_trace_if_c");
    },
    bindEvent: function(){
        var C = this, A, B;
        A = this.login.username.children("input:eq(0)").add(this.login.password.children("input:eq(0)"));
        A.focus(function(){
            C.caseInput($(this).parent(), "insert");
        }).blur(function(){
			C.clearEmpty(this);
            C.caseInput($(this).parent(), "default");
        }).keyup(function() {
			C.clearEmpty(this);
		})
        this.login.loginbut.click(function(){
            C.loginIn();
			return false;
        });
        this.login.enterreg.click(function(){
            C.gotoReg();
			return false;
        });
        this.loginTrace.onShow = function(){
            C.pub.unbind("keypress");
            C.bindKeyEvent(13, function(){
                C.login.loginbut.click();
            });
        };
        this.reg.username.children("input:eq(0)").focus(function(){
            C.caseInput($(this).parent(), "insert", "4-16\u4e2a\u5b57\u7b26, \u7b2c\u4e00\u4f4d\u975e\u6570\u5b57");
            C.reg.usernameMsg = null;
            C.reg.usernameHasReg = null;
        }).blur(function(){
			C.clearEmpty(this);
            C.checkName(false, "username");
        }).keyup(function() {
			C.clearEmpty(this);
		});
        this.reg.password.children("input:eq(0)").focus(function(){
            C.caseInput($(this).parent(), "insert", false, true);
        }).blur(function(){
			C.clearEmpty(this);
            C.caseInput(C.reg.password, "default", false, true);
        }).keyup(function(){
			C.clearEmpty(this);
			C.checkPassWord();
        });
        this.reg.checkpass.children("input:eq(0)").focus(function(){
            C.caseInput($(this).parent(), "insert", "\u91cd\u590d\u521a\u624d\u8f93\u5165\u7684\u5bc6\u7801");
        }).blur(function(){
			C.clearEmpty(this);
            C.checkAgainPass();
        }).keyup(function() {
			C.clearEmpty(this);
		})
        this.reg.mail.children("input:eq(0)").focus(function(){
            C.caseInput($(this).parent(), "insert", "\u8f93\u5165\u6b63\u786e\u7684\u90ae\u7bb1\u5730\u5740");
            C.reg.mailMsg = null;
            C.reg.mailHasReg = null;
        }).blur(function(){
			C.clearEmpty(this);
            C.checkMail();
        }).keyup(function() {
			C.clearEmpty(this);
		})
        this.reg.regbut.click(function(){
            C.regIn();
			return false;
        });
        this.regTrace.onShow = function(){
            C.pub.unbind("keypress");
            C.bindKeyEvent(13, function(){
                C.reg.regbut.click();
            });
        };
    },
    bindKeyEvent: function(K, B){
        var A = this;
        this.pub.bind("keypress", function(C){
            if (C.keyCode == K) {
                A.pub.unbind("keypress");
                B();
            }
        });
    },
    checkName: function(E, C){
        if (C != "username" && C != "nickname") {
            return;
        }
        var G = (function(){
            switch (C) {
                case "username":
                    return "\u7528\u6237\u540d";
                    break;
                case "nickname":
                    return "\u6635\u79f0";
                    break;
            }
        })(), H = this.reg[C], A = H.children("input"), B, D = this, F;
        this.reg[C + "Msg"] = null; 
        this.reg[C + "HasReg"] = null;
        if (A.val().replace(/\s*/, "") == "") {
            F = G + "\u4e0d\u80fd\u4e3a\u7a7a";
            this.caseInput(H, "default");
            return this.reg[C + "Msg"] = F;
        }
		var namelength = (function() {
			var name = A.val(),
				length = 0;
			for(var i=0; i<name.length; i++) {
				if(/[\u4e00-\u9fa5]/.test(name.charAt(i))) length+=2;
				else length+=1;
			}
			return length;
		})();
        if (namelength < 4 || namelength > 16) {
            F = "4-16字符 (1汉字等于2字符)";
            this.caseInput(H, "error", F);
            return this.reg[C + "Msg"] = "\u7528\u6237\u540d\u5e94\u4e3a" + F;
        }
        if (/^\d{1}.+?$/.test(A.val())) {
            F = "\u7b2c\u4e00\u4f4d\u4e0d\u80fd\u4e3a\u6570\u5b57";
            this.caseInput(H, "error", F);
            return this.reg[C + "Msg"] = "\u7528\u6237\u540d" + F;
        }
        if (!/^[\u4e00-\u9fa5\w]*$/.test(A.val())) {
            F = "\u4e0d\u80fd\u5305\u542b\u4e0b\u5212\u7ebf\u5916\u7684\u7279\u6b8a\u7b26\u53f7";
            this.caseInput(H, "error", F);
            return this.reg[C + "Msg"] = "\u7528\u6237\u540d" + F;
        }
        B = A.val();
        this.caseInput(H, "loading", "\u6b63\u5728\u68c0\u6d4b\u53ef\u7528\u6027");
        $.getJSON("http://u.zhubajie.com/register/check?random="+this.getRadomNumber()+"&mode=check" + C + "&" + C + "=" + encodeURIComponent(B) + "&jsoncallback=?", function(A){
			if (A.state == 1) {
                D.caseInput(H, "ok", A.msg);
                D.reg[C + "Msg"] = A.msg;
                D.reg[C + "HasReg"] = true;
            }
            else {
                D.caseInput(H, "error", A.msg);
                D.reg[C + "Msg"] = A.msg;
                D.reg[C + "HasReg"] = false;
            }
            if (E) {
                D.regIn();
            }
        });
    },
    checkPassWord: function(){
        var C = this.reg.password, B = this.reg.passking.find("span"), A = C.children("input"), 
			password = A.val().replace(/\s*/g, ""),	//密码
			passleng = password.length, 			//密码长度
			passmode = (function() {				//计算密码组合种类
				var num = 0;
				if(/[0-9]+/.test(password)) num++
				if(/[a-zA-Z]+/.test(password)) num++
				if(/[^0-9a-zA-Z\s\u4e00-\u9fa5]+/.test(password)) num++
				return num;
			})();
		B.text("");
        B.removeClass("at_current");
        if (password == "") return;
		//密码不超过8位，弱
		if (passleng < 8) {
            B.eq(0).addClass("at_current");
			B.eq(0).text("弱");
            return;
		}
		//任何字符重复3次以上，并密码字符种类只有一种，弱
		if(/(?:(.)\1{3,})/.test(password) && passmode == 1) {
            B.eq(0).addClass("at_current");
			B.eq(0).text("弱");
            return;
		}
		//密码种类超过8位存在两种 或 没有3次以上重复超过8位 中
		if(passmode == 2 || (!/(?:(.)\1{3,})/.test(password)&&passleng >=8&&passmode == 1)) {
            B.slice(0,2).addClass("at_current");
			B.eq(1).text("中");
            return;
		}
		//不管是否重复字符，密码种类存在三种，长度大于等于8，强
		if(passmode == 3) {
        	B.addClass("at_current");
			B.eq(2).text("强");
		}
    },
    checkPassWordUse: function(){
        var C = this.reg.password, B = C.children("input"), A = this.reg.username.children("input").val();
        if (B.val().replace(/\s*/, "") == "") {
            return "\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a";
        }
        if (!/^.{6,16}$/.test(B.val())) {
            return "\u5bc6\u7801\u5e94\u4e3a6-16\u4e2a\u5b57\u7b26";
        }
        if (/^(.)\1*$/.test(B.val())) {
            return "\u5bc6\u7801\u592a\u8fc7\u7b80\u5355";
        }
        if (B.val().indexOf(A) >= 0) {
            return "\u5bc6\u7801\u4e0d\u80fd\u5305\u542b\u7528\u6237\u540d";
        }
        return true;
    },
    checkAgainPass: function(){
        var D = this.reg.checkpass, A = D.children("input"), C = this.reg.password.children("input").val().replace(/\s*/, ""), B;
        if (A.val().replace(/\s*/, "") == "" || this.reg.password.hasClass("t_1_e")) {
            B = "\u786e\u8ba4\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a";
            this.caseInput(D, "default");
            return B;
        }
        if (A.val() != C) {
            B = "\u4e24\u6b21\u5bc6\u7801\u8f93\u5165\u5fc5\u987b\u76f8\u540c";
            this.caseInput(D, "error", B);
            return B;
        }
        this.caseInput(D, "ok", "\u9a8c\u8bc1\u901a\u8fc7");
        return true;
    },
    checkMail: function(C){
        var E = this.reg.mail, A = E.children("input"), D, B = this;
        this.reg.mailMsg = null;
        this.reg.mailHasReg = null;
        if (A.val().replace(/\s*/, "") == "") {
            D = "\u90ae\u7bb1\u5730\u5740\u4e0d\u80fd\u4e3a\u7a7a";
            this.caseInput(E, "default");
            return this.reg.mailMsg = D;
        }
        if (!/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(A.val())) {
            D = "\u8bf7\u8f93\u5165\u6b63\u786e\u7684\u90ae\u7bb1\u5730\u5740";
            this.caseInput(E, "error", D);
            return this.reg.mail.Msg = D;
        }
        this.caseInput(E, "loading", "\u6b63\u5728\u68c0\u6d4b\u53ef\u7528\u6027");
        $.getJSON("http://u.zhubajie.com/register/check?random="+this.getRadomNumber()+"&mode=checkemail&email=" + A.val() + "&jsoncallback=?", function(A){
            if (A.state == 1) {
                B.caseInput(E, "ok", A.msg);
                B.reg.mailMsg = A.msg;
                B.reg.mailHasReg = true;
            }
            else {
                B.caseInput(E, "error", A.msg);
                B.reg.mailMsg = A.msg;
                B.reg.mailHasReg = false;
            }
            if (C) {
                B.regIn();
            }
        });
    },
    caseInput: function(E, A, C, D){
        var A = String(/.?/.exec(A)), B;
        E.attr("className", "at_text t_1_" + A);
        if (!D) {
            E.children("em").remove();
        }
        if (C) {
            B = (A == "l") ? "at_write" : "m_3_" + A;
            E.append("<em><u class=\"at_msg " + B + "\"></u>" + C + "</em>");
        }
    },
	clearEmpty: function(O) {
		O.value = O.value.replace(/\s*/g,"");
	},
    openLogin: function(msg){
		if (msg && typeof(msg) == "string") {
			this.login.msgbox.text(msg);
			this.login.msgbox.parent().show();
		} else this.login.msgbox.parent().hide();
        this.loginTrace.setDisplay("show");
    },
    gotoReg: function(msg){
		if (msg && typeof(msg) == "string") {
			this.reg.msgbox.text(msg);
			this.reg.msgbox.parent().show();
		} else this.reg.msgbox.parent().hide();
        this.regTrace.setDisplay("show");
        this.loginTrace.setDisplay("hide");
        this.reg.username.children("input:eq(0)").focus();
    },
    showInfos: function(G, I, H, F){
        var D, A, E, B, C = this;
        D = (function(){
            if (G == "login") {
                return C.loginTrace;
            }
            else {
                if (G == "reg") {
                    return C.regTrace;
                }
            }
        })();
        this.infoBox.html(I);
        if (H) {
            A = [this.infoTrace.getButton("返回", "gray", function(){
                D.setDisplay("show");
                C.infoTrace.setDisplay("hide");
            })];
        } else {
            A = [this.infoTrace.getButton("\u786e\u5b9a", "gray", "close")];
            B = window.setTimeout(function(){
                window.clearTimeout(B);
                C.infoTrace.setDisplay("hide");
            }, 1500);
        }
        if (F) {
            E = this.infoBox.parent().children("u.at_msg");
            E.attr("className", "at_msg m_1_" + String(/.?/.exec(F)));
        }
        this.infoTrace.setContact(null, null, A);
        this.infoTrace.setDisplay("show");
        D.setDisplay("hide");
    },
    loginIn: function(){
        var A, F, D, C = this, E;
        A = this.login.username.children("input:eq(0)").val();
        F = this.login.password.children("input:eq(0)").val();
        D = this.login.autologin.attr("checked");
        if (A.replace(/\s*/i, "") == "" || F.replace(/\s*/i, "") == "") {
            E = "\u7528\u6237\u540d\u6216\u5bc6\u7801\u4e0d\u80fd\u4e3a\u7a7a";
            this.showInfos("login", E, true, "info");
            try { C.onLoginError(E) } catch (B) {}
            return;
        }
        at_loading.show();
        $.getJSON(path+"/user/dologin?url=1&random="+this.getRadomNumber()+"&u=" + encodeURIComponent(A) + "&p=" + F, function(B){
			at_loading.hide();
			if (B.state == 1) {
				var cookies = B.cookie;
            	var c = cookies.split('|');
            	$.cookie("nickname", c[0],{path:'/xunshibao-web'});
            	$.cookie("userid", c[1],{path:'/xunshibao-web'});
            	$.cookie("msgcount", c[2],{path:'/xunshibao-web'});
                C.showInfos("login", B.msg, false, "ok");
                try { 
					C.onLoginOk();
				} catch (A) {}
            }
            else {
                C.showInfos("login", B.msg, true, "error");
                try {C.onLoginError(B.msg)} catch (A) {}
            }
        });
		return false;
    },
    regIn: function(){
        var B, J, D, A, G, F = true, I = "", H = this;
        B = this.reg.username.children("input:eq(0)");
        J = this.checkPassWordUse();
        D = this.checkAgainPass();
        A = this.reg.mail.children("input:eq(0)");
        G = this.reg.agreement.attr("checked");
        if (B.val().replace(/\s*/, "") == "") {
            this.checkName(false, "username");
        }
        else {
            if (this.reg.usernameHasReg == null) {
                this.checkName(true, "username");
                return;
            }
        }
        if (A.val().replace(/\s*/, "") == "") {
            this.checkMail();
        }
        else {
            if (this.reg.mailHasReg == null) {
                this.checkMail(true);
                return;
            }
        }
        if (!this.reg.username.hasClass("t_1_o")) {
            I += this.reg.usernameMsg + "<br>";
            F = false;
        }
        if (J != true) {
            I += J + "<br>";
            F = false;
        }
        if (D != true) {
            I += D + "<br>";
            F = false;
        }
        if (!this.reg.mail.hasClass("t_1_o")) {
            I += this.reg.mailMsg + "<br>";
            F = false;
        }
        if (G != true) {
            I += "\u8bf7\u5148\u67e5\u770b\u5e76\u540c\u610f\u300a\u732a\u516b\u6212\u7f51\u7528\u6237\u670d\u52a1\u534f\u8bae\u300b";
            F = false;
        }
        if (F == false) {
            this.showInfos("reg", I, true, "error");
            try {
                H.onRegError(I);
            } 
            catch (C) {
            }
            return;
        }
        at_loading.show();
        B = B.val();
        J = this.reg.checkpass.children("input:eq(0)").val();
        A = A.val();
        $.getJSON("http://u.zhubajie.com/register/regapi?p_url=1&random="+this.getRadomNumber()+"&username=" + encodeURIComponent(B) + "&password=" + J + "&mail=" + A + "&jsoncallback=?", function(B){
			at_loading.hide();
			if (B.state == 1) {
                H.showInfos("reg", B.msg, false, "ok");
                try {
					H.onRegOk();
				} catch (A) {}
            } else {
                H.showInfos("reg", B.msg, true, "error");
                try {H.onRegError(B.msg)} catch (A) {}
            }
        });
    }
};
